<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Customizing authentication in Django &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="User authentication in Django" href="index.html" />
    <link rel="next" title="Django’s cache framework" href="../cache.html" />
    <link rel="prev" title="Password management in Django" href="passwords.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="passwords.html" title="Password management in Django">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="../cache.html" title="Django&amp;#8217;s cache framework">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-auth-customizing">
            
  <div class="section" id="s-customizing-authentication-in-django">
<span id="customizing-authentication-in-django"></span><h1>Customizing authentication in Django<a class="headerlink" href="#customizing-authentication-in-django" title="Permalink to this headline">¶</a></h1>
<p>The authentication that comes with Django is good enough for most common cases,
but you may have needs not met by the out-of-the-box defaults. To customize
authentication to your projects needs involves understanding what points of the
provided system are extensible or replaceable. This document provides details
about how the auth system can be customized.</p>
<p><a class="reference internal" href="#authentication-backends"><em>Authentication backends</em></a> provide an extensible
system for when a username and password stored with the User model need
to be authenticated against a different service than Django&#8217;s default.</p>
<p>You can give your models <a class="reference internal" href="#custom-permissions"><em>custom permissions</em></a> that can be
checked through Django&#8217;s authorization system.</p>
<p>You can <a class="reference internal" href="#extending-user"><em>extend</em></a> the default User model, or <a class="reference internal" href="#auth-custom-user"><em>substitute</em></a> a completely customized model.</p>
<div class="section" id="s-other-authentication-sources">
<span id="s-authentication-backends"></span><span id="other-authentication-sources"></span><span id="authentication-backends"></span><h2>Other authentication sources<a class="headerlink" href="#other-authentication-sources" title="Permalink to this headline">¶</a></h2>
<p>There may be times you have the need to hook into another authentication source
&#8211; that is, another source of usernames and passwords or authentication
methods.</p>
<p>For example, your company may already have an LDAP setup that stores a username
and password for every employee. It&#8217;d be a hassle for both the network
administrator and the users themselves if users had separate accounts in LDAP
and the Django-based applications.</p>
<p>So, to handle situations like this, the Django authentication system lets you
plug in other authentication sources. You can override Django&#8217;s default
database-based scheme, or you can use the default system in tandem with other
systems.</p>
<p>See the <a class="reference internal" href="../../ref/contrib/auth.html#authentication-backends-reference"><em>authentication backend reference</em></a> for information on the authentication
backends included with Django.</p>
<div class="section" id="s-specifying-authentication-backends">
<span id="specifying-authentication-backends"></span><h3>Specifying authentication backends<a class="headerlink" href="#specifying-authentication-backends" title="Permalink to this headline">¶</a></h3>
<p>Behind the scenes, Django maintains a list of &#8220;authentication backends&#8221; that it
checks for authentication. When somebody calls
<a class="reference internal" href="default.html#django.contrib.auth.authenticate" title="django.contrib.auth.authenticate"><tt class="xref py py-func docutils literal"><span class="pre">django.contrib.auth.authenticate()</span></tt></a> &#8211; as described in <a class="reference internal" href="default.html#how-to-log-a-user-in"><em>How to log
a user in</em></a> &#8211; Django tries authenticating across
all of its authentication backends. If the first authentication method fails,
Django tries the second one, and so on, until all backends have been attempted.</p>
<p>The list of authentication backends to use is specified in the
<a class="reference internal" href="../../ref/settings.html#std:setting-AUTHENTICATION_BACKENDS"><tt class="xref std std-setting docutils literal"><span class="pre">AUTHENTICATION_BACKENDS</span></tt></a> setting. This should be a tuple of Python
path names that point to Python classes that know how to authenticate. These
classes can be anywhere on your Python path.</p>
<p>By default, <a class="reference internal" href="../../ref/settings.html#std:setting-AUTHENTICATION_BACKENDS"><tt class="xref std std-setting docutils literal"><span class="pre">AUTHENTICATION_BACKENDS</span></tt></a> is set to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="s">&#39;django.contrib.auth.backends.ModelBackend&#39;</span><span class="p">,)</span>
</pre></div>
</div>
<p>That&#8217;s the basic authentication backend that checks the Django users database
and queries the built-in permissions. It does not provide protection against
brute force attacks via any rate limiting mechanism. You may either implement
your own rate limiting mechanism in a custom auth backend, or use the
mechanisms provided by most Web servers.</p>
<p>The order of <a class="reference internal" href="../../ref/settings.html#std:setting-AUTHENTICATION_BACKENDS"><tt class="xref std std-setting docutils literal"><span class="pre">AUTHENTICATION_BACKENDS</span></tt></a> matters, so if the same
username and password is valid in multiple backends, Django will stop
processing at the first positive match.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Once a user has authenticated, Django stores which backend was used to
authenticate the user in the user&#8217;s session, and re-uses the same backend
for the duration of that session whenever access to the currently
authenticated user is needed. This effectively means that authentication
sources are cached on a per-session basis, so if you change
<a class="reference internal" href="../../ref/settings.html#std:setting-AUTHENTICATION_BACKENDS"><tt class="xref std std-setting docutils literal"><span class="pre">AUTHENTICATION_BACKENDS</span></tt></a>, you&#8217;ll need to clear out session data if
you need to force users to re-authenticate using different methods. A simple
way to do that is simply to execute <tt class="docutils literal"><span class="pre">Session.objects.all().delete()</span></tt>.</p>
</div>
<div class="versionadded">
<span class="title">New in Django 1.6:</span> <p>If a backend raises a <a class="reference internal" href="../../ref/exceptions.html#django.core.exceptions.PermissionDenied" title="django.core.exceptions.PermissionDenied"><tt class="xref py py-class docutils literal"><span class="pre">PermissionDenied</span></tt></a>
exception, authentication will immediately fail. Django won&#8217;t check the
backends that follow.</p>
</div>
</div>
<div class="section" id="s-writing-an-authentication-backend">
<span id="writing-an-authentication-backend"></span><h3>Writing an authentication backend<a class="headerlink" href="#writing-an-authentication-backend" title="Permalink to this headline">¶</a></h3>
<p>An authentication backend is a class that implements two required methods:
<tt class="docutils literal"><span class="pre">get_user(user_id)</span></tt> and <tt class="docutils literal"><span class="pre">authenticate(**credentials)</span></tt>, as well as a set of
optional permission related <a class="reference internal" href="#authorization-methods"><em>authorization methods</em></a>.</p>
<p>The <tt class="docutils literal"><span class="pre">get_user</span></tt> method takes a <tt class="docutils literal"><span class="pre">user_id</span></tt> &#8211; which could be a username,
database ID or whatever, but has to be the primary key of your <tt class="docutils literal"><span class="pre">User</span></tt> object
&#8211; and returns a <tt class="docutils literal"><span class="pre">User</span></tt> object.</p>
<p>The <tt class="docutils literal"><span class="pre">authenticate</span></tt> method takes credentials as keyword arguments. Most of
the time, it&#8217;ll just look like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyBackend</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">authenticate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">password</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="c"># Check the username/password and return a User.</span>
        <span class="o">...</span>
</pre></div>
</div>
<p>But it could also authenticate a token, like so:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyBackend</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">authenticate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">token</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="c"># Check the token and return a User.</span>
        <span class="o">...</span>
</pre></div>
</div>
<p>Either way, <tt class="docutils literal"><span class="pre">authenticate</span></tt> should check the credentials it gets, and it
should return a <tt class="docutils literal"><span class="pre">User</span></tt> object that matches those credentials, if the
credentials are valid. If they&#8217;re not valid, it should return <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
<p>The Django admin system is tightly coupled to the Django <tt class="docutils literal"><span class="pre">User</span></tt> object
described at the beginning of this document. For now, the best way to deal with
this is to create a Django <tt class="docutils literal"><span class="pre">User</span></tt> object for each user that exists for your
backend (e.g., in your LDAP directory, your external SQL database, etc.) You
can either write a script to do this in advance, or your <tt class="docutils literal"><span class="pre">authenticate</span></tt>
method can do it the first time a user logs in.</p>
<p>Here&#8217;s an example backend that authenticates against a username and password
variable defined in your <tt class="docutils literal"><span class="pre">settings.py</span></tt> file and creates a Django <tt class="docutils literal"><span class="pre">User</span></tt>
object the first time a user authenticates:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="n">User</span><span class="p">,</span> <span class="n">check_password</span>

<span class="k">class</span> <span class="nc">SettingsBackend</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Authenticate against the settings ADMIN_LOGIN and ADMIN_PASSWORD.</span>

<span class="sd">    Use the login name, and a hash of the password. For example:</span>

<span class="sd">    ADMIN_LOGIN = &#39;admin&#39;</span>
<span class="sd">    ADMIN_PASSWORD = &#39;sha1$4e987$afbcf42e21bd417fb71db8c66b321e9fc33051de&#39;</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">authenticate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">password</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="n">login_valid</span> <span class="o">=</span> <span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">ADMIN_LOGIN</span> <span class="o">==</span> <span class="n">username</span><span class="p">)</span>
        <span class="n">pwd_valid</span> <span class="o">=</span> <span class="n">check_password</span><span class="p">(</span><span class="n">password</span><span class="p">,</span> <span class="n">settings</span><span class="o">.</span><span class="n">ADMIN_PASSWORD</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">login_valid</span> <span class="ow">and</span> <span class="n">pwd_valid</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">user</span> <span class="o">=</span> <span class="n">User</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">username</span><span class="o">=</span><span class="n">username</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">User</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
                <span class="c"># Create a new user. Note that we can set password</span>
                <span class="c"># to anything, because it won&#39;t be checked; the password</span>
                <span class="c"># from settings.py will.</span>
                <span class="n">user</span> <span class="o">=</span> <span class="n">User</span><span class="p">(</span><span class="n">username</span><span class="o">=</span><span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="o">=</span><span class="s">&#39;get from settings.py&#39;</span><span class="p">)</span>
                <span class="n">user</span><span class="o">.</span><span class="n">is_staff</span> <span class="o">=</span> <span class="bp">True</span>
                <span class="n">user</span><span class="o">.</span><span class="n">is_superuser</span> <span class="o">=</span> <span class="bp">True</span>
                <span class="n">user</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
            <span class="k">return</span> <span class="n">user</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">get_user</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">user_id</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">User</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="n">user_id</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">User</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">None</span>
</pre></div>
</div>
</div>
<div class="section" id="s-handling-authorization-in-custom-backends">
<span id="s-authorization-methods"></span><span id="handling-authorization-in-custom-backends"></span><span id="authorization-methods"></span><h3>Handling authorization in custom backends<a class="headerlink" href="#handling-authorization-in-custom-backends" title="Permalink to this headline">¶</a></h3>
<p>Custom auth backends can provide their own permissions.</p>
<p>The user model will delegate permission lookup functions
(<a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User.get_group_permissions" title="django.contrib.auth.models.User.get_group_permissions"><tt class="xref py py-meth docutils literal"><span class="pre">get_group_permissions()</span></tt></a>,
<a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User.get_all_permissions" title="django.contrib.auth.models.User.get_all_permissions"><tt class="xref py py-meth docutils literal"><span class="pre">get_all_permissions()</span></tt></a>,
<a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User.has_perm" title="django.contrib.auth.models.User.has_perm"><tt class="xref py py-meth docutils literal"><span class="pre">has_perm()</span></tt></a>, and
<a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User.has_module_perms" title="django.contrib.auth.models.User.has_module_perms"><tt class="xref py py-meth docutils literal"><span class="pre">has_module_perms()</span></tt></a>) to any
authentication backend that implements these functions.</p>
<p>The permissions given to the user will be the superset of all permissions
returned by all backends. That is, Django grants a permission to a user that
any one backend grants.</p>
<p>The simple backend above could implement permissions for the magic admin
fairly simply:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">SettingsBackend</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="k">def</span> <span class="nf">has_perm</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">user_obj</span><span class="p">,</span> <span class="n">perm</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">user_obj</span><span class="o">.</span><span class="n">username</span> <span class="o">==</span> <span class="n">settings</span><span class="o">.</span><span class="n">ADMIN_LOGIN</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">False</span>
</pre></div>
</div>
<p>This gives full permissions to the user granted access in the above example.
Notice that in addition to the same arguments given to the associated
<a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">django.contrib.auth.models.User</span></tt></a> functions, the backend auth functions
all take the user object, which may be an anonymous user, as an argument.</p>
<p>A full authorization implementation can be found in the <tt class="docutils literal"><span class="pre">ModelBackend</span></tt> class
in <a class="reference external" href="https://github.com/django/django/blob/master/django/contrib/auth/backends.py">django/contrib/auth/backends.py</a>, which is the default backend and queries
the <tt class="docutils literal"><span class="pre">auth_permission</span></tt> table most of the time. If you wish to provide
custom behavior for only part of the backend API, you can take advantage of
Python inheritance and subclass <tt class="docutils literal"><span class="pre">ModelBackend</span></tt> instead of implementing the
complete API in a custom backend.</p>
<div class="section" id="s-authorization-for-anonymous-users">
<span id="s-anonymous-auth"></span><span id="authorization-for-anonymous-users"></span><span id="anonymous-auth"></span><h4>Authorization for anonymous users<a class="headerlink" href="#authorization-for-anonymous-users" title="Permalink to this headline">¶</a></h4>
<p>An anonymous user is one that is not authenticated i.e. they have provided no
valid authentication details. However, that does not necessarily mean they are
not authorized to do anything. At the most basic level, most Web sites
authorize anonymous users to browse most of the site, and many allow anonymous
posting of comments etc.</p>
<p>Django&#8217;s permission framework does not have a place to store permissions for
anonymous users. However, the user object passed to an authentication backend
may be an <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.AnonymousUser" title="django.contrib.auth.models.AnonymousUser"><tt class="xref py py-class docutils literal"><span class="pre">django.contrib.auth.models.AnonymousUser</span></tt></a> object, allowing
the backend to specify custom authorization behavior for anonymous users. This
is especially useful for the authors of re-usable apps, who can delegate all
questions of authorization to the auth backend, rather than needing settings,
for example, to control anonymous access.</p>
</div>
<div class="section" id="s-authorization-for-inactive-users">
<span id="s-inactive-auth"></span><span id="authorization-for-inactive-users"></span><span id="inactive-auth"></span><h4>Authorization for inactive users<a class="headerlink" href="#authorization-for-inactive-users" title="Permalink to this headline">¶</a></h4>
<p>An inactive user is a one that is authenticated but has its attribute
<tt class="docutils literal"><span class="pre">is_active</span></tt> set to <tt class="docutils literal"><span class="pre">False</span></tt>. However this does not mean they are not
authorized to do anything. For example they are allowed to activate their
account.</p>
<p>The support for anonymous users in the permission system allows for a scenario
where anonymous users have permissions to do something while inactive
authenticated users do not.</p>
<p>Do not forget to test for the <tt class="docutils literal"><span class="pre">is_active</span></tt> attribute of the user in your own
backend permission methods.</p>
</div>
<div class="section" id="s-handling-object-permissions">
<span id="handling-object-permissions"></span><h4>Handling object permissions<a class="headerlink" href="#handling-object-permissions" title="Permalink to this headline">¶</a></h4>
<p>Django&#8217;s permission framework has a foundation for object permissions, though
there is no implementation for it in the core. That means that checking for
object permissions will always return <tt class="docutils literal"><span class="pre">False</span></tt> or an empty list (depending on
the check performed). An authentication backend will receive the keyword
parameters <tt class="docutils literal"><span class="pre">obj</span></tt> and <tt class="docutils literal"><span class="pre">user_obj</span></tt> for each object related authorization
method and can return the object level permission as appropriate.</p>
</div>
</div>
</div>
<div class="section" id="s-custom-permissions">
<span id="s-id1"></span><span id="custom-permissions"></span><span id="id1"></span><h2>Custom permissions<a class="headerlink" href="#custom-permissions" title="Permalink to this headline">¶</a></h2>
<p>To create custom permissions for a given model object, use the <tt class="docutils literal"><span class="pre">permissions</span></tt>
<a class="reference internal" href="../db/models.html#meta-options"><em>model Meta attribute</em></a>.</p>
<p>This example Task model creates three custom permissions, i.e., actions users
can or cannot do with Task instances, specific to your application:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Task</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">permissions</span> <span class="o">=</span> <span class="p">(</span>
            <span class="p">(</span><span class="s">&quot;view_task&quot;</span><span class="p">,</span> <span class="s">&quot;Can see available tasks&quot;</span><span class="p">),</span>
            <span class="p">(</span><span class="s">&quot;change_task_status&quot;</span><span class="p">,</span> <span class="s">&quot;Can change the status of tasks&quot;</span><span class="p">),</span>
            <span class="p">(</span><span class="s">&quot;close_task&quot;</span><span class="p">,</span> <span class="s">&quot;Can remove a task by setting its status as closed&quot;</span><span class="p">),</span>
        <span class="p">)</span>
</pre></div>
</div>
<p>The only thing this does is create those extra permissions when you run
<a class="reference internal" href="../../ref/django-admin.html#django-admin-migrate"><tt class="xref std std-djadmin docutils literal"><span class="pre">manage.py</span> <span class="pre">migrate</span></tt></a>. Your code is in charge of checking the
value of these permissions when a user is trying to access the functionality
provided by the application (viewing tasks, changing the status of tasks,
closing tasks.) Continuing the above example, the following checks if a user may
view tasks:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">user</span><span class="o">.</span><span class="n">has_perm</span><span class="p">(</span><span class="s">&#39;app.view_task&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-extending-the-existing-user-model">
<span id="s-extending-user"></span><span id="extending-the-existing-user-model"></span><span id="extending-user"></span><h2>Extending the existing User model<a class="headerlink" href="#extending-the-existing-user-model" title="Permalink to this headline">¶</a></h2>
<p>There are two ways to extend the default
<a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a> model without substituting your own
model. If the changes you need are purely behavioral, and don&#8217;t require any
change to what is stored in the database, you can create a <a class="reference internal" href="../db/models.html#proxy-models"><em>proxy model</em></a> based on <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a>. This
allows for any of the features offered by proxy models including default
ordering, custom managers, or custom model methods.</p>
<p>If you wish to store information related to <tt class="docutils literal"><span class="pre">User</span></tt>, you can use a <a class="reference internal" href="../../ref/models/fields.html#ref-onetoone"><em>one-to-one
relationship</em></a> to a model containing the fields for
additional information. This one-to-one model is often called a profile model,
as it might store non-auth related information about a site user. For example
you might create an Employee model:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="n">User</span>

<span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">OneToOneField</span><span class="p">(</span><span class="n">User</span><span class="p">)</span>
    <span class="n">department</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>Assuming an existing Employee Fred Smith who has both a User and Employee
model, you can access the related information using Django&#8217;s standard related
model conventions:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">u</span> <span class="o">=</span> <span class="n">User</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">username</span><span class="o">=</span><span class="s">&#39;fsmith&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">freds_department</span> <span class="o">=</span> <span class="n">u</span><span class="o">.</span><span class="n">employee</span><span class="o">.</span><span class="n">department</span>
</pre></div>
</div>
<p>To add a profile model&#8217;s fields to the user page in the admin, define an
<a class="reference internal" href="../../ref/contrib/admin/index.html#django.contrib.admin.InlineModelAdmin" title="django.contrib.admin.InlineModelAdmin"><tt class="xref py py-class docutils literal"><span class="pre">InlineModelAdmin</span></tt></a> (for this example, we&#8217;ll use a
<a class="reference internal" href="../../ref/contrib/admin/index.html#django.contrib.admin.StackedInline" title="django.contrib.admin.StackedInline"><tt class="xref py py-class docutils literal"><span class="pre">StackedInline</span></tt></a>) in your app&#8217;s <tt class="docutils literal"><span class="pre">admin.py</span></tt> and
add it to a <tt class="docutils literal"><span class="pre">UserAdmin</span></tt> class which is registered with the
<a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a> class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.admin</span> <span class="kn">import</span> <span class="n">UserAdmin</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="n">User</span>

<span class="kn">from</span> <span class="nn">my_user_profile_app.models</span> <span class="kn">import</span> <span class="n">Employee</span>

<span class="c"># Define an inline admin descriptor for Employee model</span>
<span class="c"># which acts a bit like a singleton</span>
<span class="k">class</span> <span class="nc">EmployeeInline</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">StackedInline</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">Employee</span>
    <span class="n">can_delete</span> <span class="o">=</span> <span class="bp">False</span>
    <span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="s">&#39;employee&#39;</span>

<span class="c"># Define a new User admin</span>
<span class="k">class</span> <span class="nc">UserAdmin</span><span class="p">(</span><span class="n">UserAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">(</span><span class="n">EmployeeInline</span><span class="p">,</span> <span class="p">)</span>

<span class="c"># Re-register UserAdmin</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">unregister</span><span class="p">(</span><span class="n">User</span><span class="p">)</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="n">UserAdmin</span><span class="p">)</span>
</pre></div>
</div>
<p>These profile models are not special in any way - they are just Django models that
happen to have a one-to-one link with a User model. As such, they do not get
auto created when a user is created, but
a <a class="reference internal" href="../../ref/signals.html#django.db.models.signals.post_save" title="django.db.models.signals.post_save"><tt class="xref py py-attr docutils literal"><span class="pre">django.db.models.signals.post_save</span></tt></a> could be used to create or update
related models as appropriate.</p>
<p>Note that using related models results in additional queries or joins to
retrieve the related data, and depending on your needs substituting the User
model and adding the related fields may be your better option.  However
existing links to the default User model within your project&#8217;s apps may justify
the extra database load.</p>
</div>
<div class="section" id="s-substituting-a-custom-user-model">
<span id="s-auth-custom-user"></span><span id="substituting-a-custom-user-model"></span><span id="auth-custom-user"></span><h2>Substituting a custom User model<a class="headerlink" href="#substituting-a-custom-user-model" title="Permalink to this headline">¶</a></h2>
<p>Some kinds of projects may have authentication requirements for which Django&#8217;s
built-in <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a> model is not always
appropriate. For instance, on some sites it makes more sense to use an email
address as your identification token instead of a username.</p>
<p>Django allows you to override the default User model by providing a value for
the <a class="reference internal" href="../../ref/settings.html#std:setting-AUTH_USER_MODEL"><tt class="xref std std-setting docutils literal"><span class="pre">AUTH_USER_MODEL</span></tt></a> setting that references a custom model:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">AUTH_USER_MODEL</span> <span class="o">=</span> <span class="s">&#39;myapp.MyUser&#39;</span>
</pre></div>
</div>
<p>This dotted pair describes the name of the Django app (which must be in your
<a class="reference internal" href="../../ref/settings.html#std:setting-INSTALLED_APPS"><tt class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></tt></a>), and the name of the Django model that you wish to
use as your User model.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>Changing <a class="reference internal" href="../../ref/settings.html#std:setting-AUTH_USER_MODEL"><tt class="xref std std-setting docutils literal"><span class="pre">AUTH_USER_MODEL</span></tt></a> has a big effect on your database
structure. It changes the tables that are available, and it will affect the
construction of foreign keys and many-to-many relationships. If you intend
to set <a class="reference internal" href="../../ref/settings.html#std:setting-AUTH_USER_MODEL"><tt class="xref std std-setting docutils literal"><span class="pre">AUTH_USER_MODEL</span></tt></a>, you should set it before creating
any migrations or running <tt class="docutils literal"><span class="pre">manage.py</span> <span class="pre">migrate</span></tt> for the first time.</p>
<p class="last">Changing this setting after you have tables created is not supported
by <a class="reference internal" href="../../ref/django-admin.html#django-admin-makemigrations"><tt class="xref std std-djadmin docutils literal"><span class="pre">makemigrations</span></tt></a> and will result in you having to manually
fix your schema, port your data from the old user table, and possibly
manually reapply some migrations.</p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>Due to limitations of Django&#8217;s dynamic dependency feature for swappable
models, you must ensure that the model referenced by <a class="reference internal" href="../../ref/settings.html#std:setting-AUTH_USER_MODEL"><tt class="xref std std-setting docutils literal"><span class="pre">AUTH_USER_MODEL</span></tt></a>
is created in the first migration of its app (usually called <tt class="docutils literal"><span class="pre">0001_initial</span></tt>);
otherwise, you will have dependency issues.</p>
<p class="last">In addition, you may run into a CircularDependencyError when running your
migrations as Django won&#8217;t be able to automatically break the dependency
loop due to the dynamic dependency. If you see this error, you should
break the loop by moving the models depended on by your User model
into a second migration (you can try making two normal models that
have a ForeignKey to each other and seeing how <tt class="docutils literal"><span class="pre">makemigrations</span></tt> resolves that
circular dependency if you want to see how it&#8217;s usually done)</p>
</div>
<div class="section" id="s-referencing-the-user-model">
<span id="referencing-the-user-model"></span><h3>Referencing the User model<a class="headerlink" href="#referencing-the-user-model" title="Permalink to this headline">¶</a></h3>
<p>If you reference <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a> directly (for
example, by referring to it in a foreign key), your code will not work in
projects where the <a class="reference internal" href="../../ref/settings.html#std:setting-AUTH_USER_MODEL"><tt class="xref std std-setting docutils literal"><span class="pre">AUTH_USER_MODEL</span></tt></a> setting has been changed to a
different User model.</p>
<dl class="function">
<dt id="django.contrib.auth.get_user_model">
<tt class="descname">get_user_model</tt>()<a class="reference internal" href="../../_modules/django/contrib/auth.html#get_user_model"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.auth.get_user_model" title="Permalink to this definition">¶</a></dt>
<dd><p>Instead of referring to <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a> directly,
you should reference the user model using
<tt class="docutils literal"><span class="pre">django.contrib.auth.get_user_model()</span></tt>. This method will return the
currently active User model &#8211; the custom User model if one is specified, or
<a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a> otherwise.</p>
<p>When you define a foreign key or many-to-many relations to the User model,
you should specify the custom model using the <a class="reference internal" href="../../ref/settings.html#std:setting-AUTH_USER_MODEL"><tt class="xref std std-setting docutils literal"><span class="pre">AUTH_USER_MODEL</span></tt></a>
setting. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Article</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">author</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">AUTH_USER_MODEL</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<span class="title">New in Django 1.7:</span> <p>When connecting to signals sent by the User model, you should specify the
custom model using the <a class="reference internal" href="../../ref/settings.html#std:setting-AUTH_USER_MODEL"><tt class="xref std std-setting docutils literal"><span class="pre">AUTH_USER_MODEL</span></tt></a> setting. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.db.models.signals</span> <span class="kn">import</span> <span class="n">post_save</span>

<span class="k">def</span> <span class="nf">post_save_receiver</span><span class="p">(</span><span class="n">signal</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="n">post_save</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">post_save_receiver</span><span class="p">,</span> <span class="n">sender</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">AUTH_USER_MODEL</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p>Generally speaking, you should reference the User model with the
<a class="reference internal" href="../../ref/settings.html#std:setting-AUTH_USER_MODEL"><tt class="xref std std-setting docutils literal"><span class="pre">AUTH_USER_MODEL</span></tt></a> setting in code that is executed at import
time. <tt class="docutils literal"><span class="pre">get_user_model()</span></tt> only works once Django has imported all models.</p>
</dd></dl>

</div>
<div class="section" id="s-specifying-a-custom-user-model">
<span id="s-specifying-custom-user-model"></span><span id="specifying-a-custom-user-model"></span><span id="specifying-custom-user-model"></span><h3>Specifying a custom User model<a class="headerlink" href="#specifying-a-custom-user-model" title="Permalink to this headline">¶</a></h3>
<div class="admonition-model-design-considerations admonition">
<p class="first admonition-title">Model design considerations</p>
<p>Think carefully before handling information not directly related to
authentication in your custom User Model.</p>
<p class="last">It may be better to store app-specific user information in a model
that has a relation with the User model. That allows each app to specify
its own user data requirements without risking conflicts with other
apps. On the other hand, queries to retrieve this related information
will involve a database join, which may have an effect on performance.</p>
</div>
<p>Django expects your custom User model to meet some minimum requirements.</p>
<ol class="arabic simple">
<li>Your model must have an integer primary key.</li>
<li>Your model must have a single unique field that can be used for
identification purposes. This can be a username, an email address,
or any other unique attribute.</li>
<li>Your model must provide a way to address the user in a &#8220;short&#8221; and
&#8220;long&#8221; form. The most common interpretation of this would be to use
the user&#8217;s given name as the &#8220;short&#8221; identifier, and the user&#8217;s full
name as the &#8220;long&#8221; identifier. However, there are no constraints on
what these two methods return - if you want, they can return exactly
the same value.</li>
</ol>
<p>The easiest way to construct a compliant custom User model is to inherit from
<a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser" title="django.contrib.auth.models.AbstractBaseUser"><tt class="xref py py-class docutils literal"><span class="pre">AbstractBaseUser</span></tt></a>.
<a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser" title="django.contrib.auth.models.AbstractBaseUser"><tt class="xref py py-class docutils literal"><span class="pre">AbstractBaseUser</span></tt></a> provides the core
implementation of a <tt class="docutils literal"><span class="pre">User</span></tt> model, including hashed passwords and tokenized
password resets. You must then provide some key implementation details:</p>
<dl class="class">
<dt id="django.contrib.auth.models.CustomUser">
<em class="property">class </em><tt class="descclassname">models.</tt><tt class="descname">CustomUser</tt><a class="headerlink" href="#django.contrib.auth.models.CustomUser" title="Permalink to this definition">¶</a></dt>
<dd><dl class="attribute">
<dt id="django.contrib.auth.models.CustomUser.USERNAME_FIELD">
<tt class="descname">USERNAME_FIELD</tt><a class="headerlink" href="#django.contrib.auth.models.CustomUser.USERNAME_FIELD" title="Permalink to this definition">¶</a></dt>
<dd><p>A string describing the name of the field on the User model that is
used as the unique identifier. This will usually be a username of
some kind, but it can also be an email address, or any other unique
identifier. The field <em>must</em> be unique (i.e., have <tt class="docutils literal"><span class="pre">unique=True</span></tt>
set in its definition).</p>
<p>In the following example, the field <tt class="docutils literal"><span class="pre">identifier</span></tt> is used
as the identifying field:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyUser</span><span class="p">(</span><span class="n">AbstractBaseUser</span><span class="p">):</span>
    <span class="n">identifier</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="o">...</span>
    <span class="n">USERNAME_FIELD</span> <span class="o">=</span> <span class="s">&#39;identifier&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.auth.models.CustomUser.REQUIRED_FIELDS">
<tt class="descname">REQUIRED_FIELDS</tt><a class="headerlink" href="#django.contrib.auth.models.CustomUser.REQUIRED_FIELDS" title="Permalink to this definition">¶</a></dt>
<dd><p>A list of the field names that will be prompted for when creating a
user via the <a class="reference internal" href="../../ref/django-admin.html#django-admin-createsuperuser"><tt class="xref std std-djadmin docutils literal"><span class="pre">createsuperuser</span></tt></a> management command. The user
will be prompted to supply a value for each of these fields. It must
include any field for which <a class="reference internal" href="../../ref/models/fields.html#django.db.models.Field.blank" title="django.db.models.Field.blank"><tt class="xref py py-attr docutils literal"><span class="pre">blank</span></tt></a> is
<tt class="docutils literal"><span class="pre">False</span></tt> or undefined and may include additional fields you want
prompted for when a user is created interactively. However, it will not
work for <a class="reference internal" href="../../ref/models/fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> fields.
<tt class="docutils literal"><span class="pre">REQUIRED_FIELDS</span></tt> has no effect in other parts of Django, like
creating a user in the admin.</p>
<p>For example, here is the partial definition for a <tt class="docutils literal"><span class="pre">User</span></tt> model that
defines two required fields - a date of birth and height:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyUser</span><span class="p">(</span><span class="n">AbstractBaseUser</span><span class="p">):</span>
    <span class="o">...</span>
    <span class="n">date_of_birth</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
    <span class="n">height</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">FloatField</span><span class="p">()</span>
    <span class="o">...</span>
    <span class="n">REQUIRED_FIELDS</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;date_of_birth&#39;</span><span class="p">,</span> <span class="s">&#39;height&#39;</span><span class="p">]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><tt class="docutils literal"><span class="pre">REQUIRED_FIELDS</span></tt> must contain all required fields on your
<tt class="docutils literal"><span class="pre">User</span></tt> model, but should <em>not</em> contain the <tt class="docutils literal"><span class="pre">USERNAME_FIELD</span></tt> or
<tt class="docutils literal"><span class="pre">password</span></tt> as these fields will always be prompted for.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.auth.models.CustomUser.is_active">
<tt class="descname">is_active</tt><a class="headerlink" href="#django.contrib.auth.models.CustomUser.is_active" title="Permalink to this definition">¶</a></dt>
<dd><p>A boolean attribute that indicates whether the user is considered
&#8220;active&#8221;.  This attribute is provided as an attribute on
<tt class="docutils literal"><span class="pre">AbstractBaseUser</span></tt> defaulting to <tt class="docutils literal"><span class="pre">True</span></tt>. How you choose to
implement it will depend on the details of your chosen auth backends.
See the documentation of the <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User.is_active" title="django.contrib.auth.models.User.is_active"><tt class="xref py py-attr docutils literal"><span class="pre">is_active</span> <span class="pre">attribute</span> <span class="pre">on</span> <span class="pre">the</span> <span class="pre">built-in</span>
<span class="pre">user</span> <span class="pre">model</span></tt></a> for details.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.CustomUser.get_full_name">
<tt class="descname">get_full_name</tt>()<a class="headerlink" href="#django.contrib.auth.models.CustomUser.get_full_name" title="Permalink to this definition">¶</a></dt>
<dd><p>A longer formal identifier for the user. A common interpretation
would be the full name of the user, but it can be any string that
identifies the user.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.CustomUser.get_short_name">
<tt class="descname">get_short_name</tt>()<a class="headerlink" href="#django.contrib.auth.models.CustomUser.get_short_name" title="Permalink to this definition">¶</a></dt>
<dd><p>A short, informal identifier for the user. A common interpretation
would be the first name of the user, but it can be any string that
identifies the user in an informal way. It may also return the same
value as <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User.get_full_name" title="django.contrib.auth.models.User.get_full_name"><tt class="xref py py-meth docutils literal"><span class="pre">django.contrib.auth.models.User.get_full_name()</span></tt></a>.</p>
</dd></dl>

</dd></dl>

<p>The following methods are available on any subclass of
<a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser" title="django.contrib.auth.models.AbstractBaseUser"><tt class="xref py py-class docutils literal"><span class="pre">AbstractBaseUser</span></tt></a>:</p>
<dl class="class">
<dt id="django.contrib.auth.models.AbstractBaseUser">
<em class="property">class </em><tt class="descclassname">models.</tt><tt class="descname">AbstractBaseUser</tt><a class="headerlink" href="#django.contrib.auth.models.AbstractBaseUser" title="Permalink to this definition">¶</a></dt>
<dd><dl class="method">
<dt id="django.contrib.auth.models.AbstractBaseUser.get_username">
<tt class="descname">get_username</tt>()<a class="headerlink" href="#django.contrib.auth.models.AbstractBaseUser.get_username" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the value of the field nominated by <tt class="docutils literal"><span class="pre">USERNAME_FIELD</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.AbstractBaseUser.is_anonymous">
<tt class="descname">is_anonymous</tt>()<a class="headerlink" href="#django.contrib.auth.models.AbstractBaseUser.is_anonymous" title="Permalink to this definition">¶</a></dt>
<dd><p>Always returns <tt class="docutils literal"><span class="pre">False</span></tt>. This is a way of differentiating
from  <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.AnonymousUser" title="django.contrib.auth.models.AnonymousUser"><tt class="xref py py-class docutils literal"><span class="pre">AnonymousUser</span></tt></a> objects.
Generally, you should prefer using
<a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser.is_authenticated" title="django.contrib.auth.models.AbstractBaseUser.is_authenticated"><tt class="xref py py-meth docutils literal"><span class="pre">is_authenticated()</span></tt></a> to this
method.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.AbstractBaseUser.is_authenticated">
<tt class="descname">is_authenticated</tt>()<a class="headerlink" href="#django.contrib.auth.models.AbstractBaseUser.is_authenticated" title="Permalink to this definition">¶</a></dt>
<dd><p>Always returns <tt class="docutils literal"><span class="pre">True</span></tt>. This is a way to tell if the user has been
authenticated. This does not imply any permissions, and doesn&#8217;t check
if the user is active - it only indicates that the user has provided a
valid username and password.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.AbstractBaseUser.set_password">
<tt class="descname">set_password</tt>(<em>raw_password</em>)<a class="headerlink" href="#django.contrib.auth.models.AbstractBaseUser.set_password" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the user&#8217;s password to the given raw string, taking care of the
password hashing. Doesn&#8217;t save the
<a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser" title="django.contrib.auth.models.AbstractBaseUser"><tt class="xref py py-class docutils literal"><span class="pre">AbstractBaseUser</span></tt></a> object.</p>
<p>When the raw_password is <tt class="docutils literal"><span class="pre">None</span></tt>, the password will be set to an
unusable password, as if
<a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser.set_unusable_password" title="django.contrib.auth.models.AbstractBaseUser.set_unusable_password"><tt class="xref py py-meth docutils literal"><span class="pre">set_unusable_password()</span></tt></a>
were used.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.6:</span> <p>In Django 1.4 and 1.5, a blank string was unintentionally stored
as an unusable password as well.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.AbstractBaseUser.check_password">
<tt class="descname">check_password</tt>(<em>raw_password</em>)<a class="headerlink" href="#django.contrib.auth.models.AbstractBaseUser.check_password" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if the given raw string is the correct password for
the user. (This takes care of the password hashing in making the
comparison.)</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.6:</span> <p>In Django 1.4 and 1.5, a blank string was unintentionally
considered to be an unusable password, resulting in this method
returning <tt class="docutils literal"><span class="pre">False</span></tt> for such a password.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.AbstractBaseUser.set_unusable_password">
<tt class="descname">set_unusable_password</tt>()<a class="headerlink" href="#django.contrib.auth.models.AbstractBaseUser.set_unusable_password" title="Permalink to this definition">¶</a></dt>
<dd><p>Marks the user as having no password set.  This isn&#8217;t the same as
having a blank string for a password.
<a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser.check_password" title="django.contrib.auth.models.AbstractBaseUser.check_password"><tt class="xref py py-meth docutils literal"><span class="pre">check_password()</span></tt></a> for this user
will never return <tt class="docutils literal"><span class="pre">True</span></tt>. Doesn&#8217;t save the
<a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser" title="django.contrib.auth.models.AbstractBaseUser"><tt class="xref py py-class docutils literal"><span class="pre">AbstractBaseUser</span></tt></a> object.</p>
<p>You may need this if authentication for your application takes place
against an existing external source such as an LDAP directory.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.AbstractBaseUser.has_usable_password">
<tt class="descname">has_usable_password</tt>()<a class="headerlink" href="#django.contrib.auth.models.AbstractBaseUser.has_usable_password" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <tt class="docutils literal"><span class="pre">False</span></tt> if
<a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser.set_unusable_password" title="django.contrib.auth.models.AbstractBaseUser.set_unusable_password"><tt class="xref py py-meth docutils literal"><span class="pre">set_unusable_password()</span></tt></a> has
been called for this user.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash">
<tt class="descname">get_session_auth_hash</tt>()<a class="headerlink" href="#django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Returns an HMAC of the password field. Used for
<a class="reference internal" href="default.html#session-invalidation-on-password-change"><em>Session invalidation on password change</em></a>.</p>
</dd></dl>

</dd></dl>

<p>You should also define a custom manager for your <tt class="docutils literal"><span class="pre">User</span></tt> model. If your
<tt class="docutils literal"><span class="pre">User</span></tt> model defines <tt class="docutils literal"><span class="pre">username</span></tt>, <tt class="docutils literal"><span class="pre">email</span></tt>, <tt class="docutils literal"><span class="pre">is_staff</span></tt>, <tt class="docutils literal"><span class="pre">is_active</span></tt>,
<tt class="docutils literal"><span class="pre">is_superuser</span></tt>, <tt class="docutils literal"><span class="pre">last_login</span></tt>, and <tt class="docutils literal"><span class="pre">date_joined</span></tt> fields the same as
Django&#8217;s default <tt class="docutils literal"><span class="pre">User</span></tt>, you can just install Django&#8217;s
<a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.UserManager" title="django.contrib.auth.models.UserManager"><tt class="xref py py-class docutils literal"><span class="pre">UserManager</span></tt></a>; however, if your <tt class="docutils literal"><span class="pre">User</span></tt>
model defines different fields, you will need to define a custom manager that
extends <a class="reference internal" href="#django.contrib.auth.models.BaseUserManager" title="django.contrib.auth.models.BaseUserManager"><tt class="xref py py-class docutils literal"><span class="pre">BaseUserManager</span></tt></a> providing two
additional methods:</p>
<dl class="class">
<dt id="django.contrib.auth.models.CustomUserManager">
<em class="property">class </em><tt class="descclassname">models.</tt><tt class="descname">CustomUserManager</tt><a class="headerlink" href="#django.contrib.auth.models.CustomUserManager" title="Permalink to this definition">¶</a></dt>
<dd><dl class="method">
<dt id="django.contrib.auth.models.CustomUserManager.create_user">
<tt class="descname">create_user</tt>(<em>*username_field*</em>, <em>password=None</em>, <em>**other_fields</em>)<a class="headerlink" href="#django.contrib.auth.models.CustomUserManager.create_user" title="Permalink to this definition">¶</a></dt>
<dd><p>The prototype of <tt class="docutils literal"><span class="pre">create_user()</span></tt> should accept the username field,
plus all required fields as arguments. For example, if your user model
uses <tt class="docutils literal"><span class="pre">email</span></tt> as the username field, and has <tt class="docutils literal"><span class="pre">date_of_birth</span></tt> as a
required field, then <tt class="docutils literal"><span class="pre">create_user</span></tt> should be defined as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">create_user</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">email</span><span class="p">,</span> <span class="n">date_of_birth</span><span class="p">,</span> <span class="n">password</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="c"># create user here</span>
    <span class="o">...</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.CustomUserManager.create_superuser">
<tt class="descname">create_superuser</tt>(<em>*username_field*</em>, <em>password</em>, <em>**other_fields</em>)<a class="headerlink" href="#django.contrib.auth.models.CustomUserManager.create_superuser" title="Permalink to this definition">¶</a></dt>
<dd><p>The prototype of <tt class="docutils literal"><span class="pre">create_superuser()</span></tt> should accept the username
field, plus all required fields as arguments. For example, if your user
model uses <tt class="docutils literal"><span class="pre">email</span></tt> as the username field, and has <tt class="docutils literal"><span class="pre">date_of_birth</span></tt>
as a required field, then <tt class="docutils literal"><span class="pre">create_superuser</span></tt> should be defined as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">create_superuser</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">email</span><span class="p">,</span> <span class="n">date_of_birth</span><span class="p">,</span> <span class="n">password</span><span class="p">):</span>
    <span class="c"># create superuser here</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>Unlike <tt class="docutils literal"><span class="pre">create_user()</span></tt>, <tt class="docutils literal"><span class="pre">create_superuser()</span></tt> <em>must</em> require the
caller to provide a password.</p>
</dd></dl>

</dd></dl>

<p><a class="reference internal" href="#django.contrib.auth.models.BaseUserManager" title="django.contrib.auth.models.BaseUserManager"><tt class="xref py py-class docutils literal"><span class="pre">BaseUserManager</span></tt></a> provides the following
utility methods:</p>
<dl class="class">
<dt id="django.contrib.auth.models.BaseUserManager">
<em class="property">class </em><tt class="descclassname">models.</tt><tt class="descname">BaseUserManager</tt><a class="headerlink" href="#django.contrib.auth.models.BaseUserManager" title="Permalink to this definition">¶</a></dt>
<dd><dl class="method">
<dt id="django.contrib.auth.models.BaseUserManager.normalize_email">
<tt class="descname">normalize_email</tt>(<em>email</em>)<a class="headerlink" href="#django.contrib.auth.models.BaseUserManager.normalize_email" title="Permalink to this definition">¶</a></dt>
<dd><p>A <tt class="docutils literal"><span class="pre">classmethod</span></tt> that normalizes email addresses by lowercasing
the domain portion of the email address.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.BaseUserManager.get_by_natural_key">
<tt class="descname">get_by_natural_key</tt>(<em>username</em>)<a class="headerlink" href="#django.contrib.auth.models.BaseUserManager.get_by_natural_key" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieves a user instance using the contents of the field
nominated by <tt class="docutils literal"><span class="pre">USERNAME_FIELD</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.BaseUserManager.make_random_password">
<tt class="descname">make_random_password</tt>(<em>length=10</em>, <em>allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'</em>)<a class="headerlink" href="#django.contrib.auth.models.BaseUserManager.make_random_password" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a random password with the given length and given string of
allowed characters. Note that the default value of <tt class="docutils literal"><span class="pre">allowed_chars</span></tt>
doesn&#8217;t contain letters that can cause user confusion, including:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">i</span></tt>, <tt class="docutils literal"><span class="pre">l</span></tt>, <tt class="docutils literal"><span class="pre">I</span></tt>, and <tt class="docutils literal"><span class="pre">1</span></tt> (lowercase letter i, lowercase
letter L, uppercase letter i, and the number one)</li>
<li><tt class="docutils literal"><span class="pre">o</span></tt>, <tt class="docutils literal"><span class="pre">O</span></tt>, and <tt class="docutils literal"><span class="pre">0</span></tt> (lowercase letter o, uppercase letter o,
and zero)</li>
</ul>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-extending-django-s-default-user">
<span id="extending-django-s-default-user"></span><h3>Extending Django&#8217;s default User<a class="headerlink" href="#extending-django-s-default-user" title="Permalink to this headline">¶</a></h3>
<p>If you&#8217;re entirely happy with Django&#8217;s <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a>
model and you just want to add some additional profile information, you could
simply subclass <tt class="docutils literal"><span class="pre">django.contrib.auth.models.AbstractUser</span></tt> and add your
custom profile fields, although we&#8217;d recommend a separate model as described in
the &#8220;Model design considerations&#8221; note of <a class="reference internal" href="#specifying-custom-user-model"><em>Specifying a custom User model</em></a>.
<tt class="docutils literal"><span class="pre">AbstractUser</span></tt> provides the full implementation of the default
<a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a> as an <a class="reference internal" href="../db/models.html#abstract-base-classes"><em>abstract model</em></a>.</p>
</div>
<div class="section" id="s-custom-users-and-the-built-in-auth-forms">
<span id="s-id2"></span><span id="custom-users-and-the-built-in-auth-forms"></span><span id="id2"></span><h3>Custom users and the built-in auth forms<a class="headerlink" href="#custom-users-and-the-built-in-auth-forms" title="Permalink to this headline">¶</a></h3>
<p>As you may expect, built-in Django&#8217;s <a class="reference internal" href="default.html#built-in-auth-forms"><em>forms</em></a> and
<a class="reference internal" href="default.html#built-in-auth-views"><em>views</em></a> make certain assumptions about the user
model that they are working with.</p>
<p>If your user model doesn&#8217;t follow the same assumptions, it may be necessary to define
a replacement form, and pass that form in as part of the configuration of the
auth views.</p>
<ul>
<li><p class="first"><a class="reference internal" href="default.html#django.contrib.auth.forms.UserCreationForm" title="django.contrib.auth.forms.UserCreationForm"><tt class="xref py py-class docutils literal"><span class="pre">UserCreationForm</span></tt></a></p>
<p>Depends on the <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a> model.
Must be re-written for any custom user model.</p>
</li>
<li><p class="first"><a class="reference internal" href="default.html#django.contrib.auth.forms.UserChangeForm" title="django.contrib.auth.forms.UserChangeForm"><tt class="xref py py-class docutils literal"><span class="pre">UserChangeForm</span></tt></a></p>
<p>Depends on the <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a> model.
Must be re-written for any custom user model.</p>
</li>
<li><p class="first"><a class="reference internal" href="default.html#django.contrib.auth.forms.AuthenticationForm" title="django.contrib.auth.forms.AuthenticationForm"><tt class="xref py py-class docutils literal"><span class="pre">AuthenticationForm</span></tt></a></p>
<p>Works with any subclass of <a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser" title="django.contrib.auth.models.AbstractBaseUser"><tt class="xref py py-class docutils literal"><span class="pre">AbstractBaseUser</span></tt></a>,
and will adapt to use the field defined in <tt class="docutils literal"><span class="pre">USERNAME_FIELD</span></tt>.</p>
</li>
<li><p class="first"><a class="reference internal" href="default.html#django.contrib.auth.forms.PasswordResetForm" title="django.contrib.auth.forms.PasswordResetForm"><tt class="xref py py-class docutils literal"><span class="pre">PasswordResetForm</span></tt></a></p>
<p>Assumes that the user model has a field named <tt class="docutils literal"><span class="pre">email</span></tt> that can be used to
identify the user and a boolean field named <tt class="docutils literal"><span class="pre">is_active</span></tt> to prevent
password resets for inactive users.</p>
</li>
<li><p class="first"><a class="reference internal" href="default.html#django.contrib.auth.forms.SetPasswordForm" title="django.contrib.auth.forms.SetPasswordForm"><tt class="xref py py-class docutils literal"><span class="pre">SetPasswordForm</span></tt></a></p>
<p>Works with any subclass of <a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser" title="django.contrib.auth.models.AbstractBaseUser"><tt class="xref py py-class docutils literal"><span class="pre">AbstractBaseUser</span></tt></a></p>
</li>
<li><p class="first"><a class="reference internal" href="default.html#django.contrib.auth.forms.PasswordChangeForm" title="django.contrib.auth.forms.PasswordChangeForm"><tt class="xref py py-class docutils literal"><span class="pre">PasswordChangeForm</span></tt></a></p>
<p>Works with any subclass of <a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser" title="django.contrib.auth.models.AbstractBaseUser"><tt class="xref py py-class docutils literal"><span class="pre">AbstractBaseUser</span></tt></a></p>
</li>
<li><p class="first"><a class="reference internal" href="default.html#django.contrib.auth.forms.AdminPasswordChangeForm" title="django.contrib.auth.forms.AdminPasswordChangeForm"><tt class="xref py py-class docutils literal"><span class="pre">AdminPasswordChangeForm</span></tt></a></p>
<p>Works with any subclass of <a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser" title="django.contrib.auth.models.AbstractBaseUser"><tt class="xref py py-class docutils literal"><span class="pre">AbstractBaseUser</span></tt></a></p>
</li>
</ul>
</div>
<div class="section" id="s-custom-users-and-django-contrib-admin">
<span id="custom-users-and-django-contrib-admin"></span><h3>Custom users and <a class="reference internal" href="../../ref/contrib/admin/index.html#module-django.contrib.admin" title="django.contrib.admin: Django's admin site."><tt class="xref py py-mod docutils literal"><span class="pre">django.contrib.admin</span></tt></a><a class="headerlink" href="#custom-users-and-django-contrib-admin" title="Permalink to this headline">¶</a></h3>
<p>If you want your custom User model to also work with Admin, your User model must
define some additional attributes and methods. These methods allow the admin to
control access of the User to admin content:</p>
<dl class="class">
<dt>
<em class="property">class </em><tt class="descclassname">models.</tt><tt class="descname">CustomUser</tt></dt>
<dd></dd></dl>

<dl class="attribute">
<dt id="django.contrib.auth.is_staff">
<tt class="descname">is_staff</tt><a class="headerlink" href="#django.contrib.auth.is_staff" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if the user is allowed to have access to the admin site.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.auth.is_active">
<tt class="descname">is_active</tt><a class="headerlink" href="#django.contrib.auth.is_active" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if the user account is currently active.</p>
</dd></dl>

<dl class="method">
<dt>
<tt class="descname">has_perm(perm, obj=None):</tt></dt>
<dd><p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if the user has the named permission. If <tt class="docutils literal"><span class="pre">obj</span></tt> is
provided, the permission needs to be checked against a specific object
instance.</p>
</dd></dl>

<dl class="method">
<dt>
<tt class="descname">has_module_perms(app_label):</tt></dt>
<dd><p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if the user has permission to access models in
the given app.</p>
</dd></dl>

<p>You will also need to register your custom User model with the admin. If
your custom User model extends <tt class="docutils literal"><span class="pre">django.contrib.auth.models.AbstractUser</span></tt>,
you can use Django&#8217;s existing <tt class="docutils literal"><span class="pre">django.contrib.auth.admin.UserAdmin</span></tt>
class. However, if your User model extends
<a class="reference internal" href="#django.contrib.auth.models.AbstractBaseUser" title="django.contrib.auth.models.AbstractBaseUser"><tt class="xref py py-class docutils literal"><span class="pre">AbstractBaseUser</span></tt></a>, you&#8217;ll need to define
a custom <tt class="docutils literal"><span class="pre">ModelAdmin</span></tt> class. It may be possible to subclass the default
<tt class="docutils literal"><span class="pre">django.contrib.auth.admin.UserAdmin</span></tt>; however, you&#8217;ll need to
override any of the definitions that refer to fields on
<tt class="docutils literal"><span class="pre">django.contrib.auth.models.AbstractUser</span></tt> that aren&#8217;t on your
custom User class.</p>
</div>
<div class="section" id="s-custom-users-and-permissions">
<span id="custom-users-and-permissions"></span><h3>Custom users and permissions<a class="headerlink" href="#custom-users-and-permissions" title="Permalink to this headline">¶</a></h3>
<p>To make it easy to include Django&#8217;s permission framework into your own User
class, Django provides <a class="reference internal" href="#django.contrib.auth.models.PermissionsMixin" title="django.contrib.auth.models.PermissionsMixin"><tt class="xref py py-class docutils literal"><span class="pre">PermissionsMixin</span></tt></a>.
This is an abstract model you can include in the class hierarchy for your User
model, giving you all the methods and database fields necessary to support
Django&#8217;s permission model.</p>
<p><a class="reference internal" href="#django.contrib.auth.models.PermissionsMixin" title="django.contrib.auth.models.PermissionsMixin"><tt class="xref py py-class docutils literal"><span class="pre">PermissionsMixin</span></tt></a> provides the following
methods and attributes:</p>
<dl class="class">
<dt id="django.contrib.auth.models.PermissionsMixin">
<em class="property">class </em><tt class="descclassname">models.</tt><tt class="descname">PermissionsMixin</tt><a class="headerlink" href="#django.contrib.auth.models.PermissionsMixin" title="Permalink to this definition">¶</a></dt>
<dd><dl class="attribute">
<dt id="django.contrib.auth.models.PermissionsMixin.is_superuser">
<tt class="descname">is_superuser</tt><a class="headerlink" href="#django.contrib.auth.models.PermissionsMixin.is_superuser" title="Permalink to this definition">¶</a></dt>
<dd><p>Boolean. Designates that this user has all permissions without
explicitly assigning them.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.PermissionsMixin.get_group_permissions">
<tt class="descname">get_group_permissions</tt>(<em>obj=None</em>)<a class="headerlink" href="#django.contrib.auth.models.PermissionsMixin.get_group_permissions" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a set of permission strings that the user has, through their
groups.</p>
<p>If <tt class="docutils literal"><span class="pre">obj</span></tt> is passed in, only returns the group permissions for
this specific object.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.PermissionsMixin.get_all_permissions">
<tt class="descname">get_all_permissions</tt>(<em>obj=None</em>)<a class="headerlink" href="#django.contrib.auth.models.PermissionsMixin.get_all_permissions" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a set of permission strings that the user has, both through
group and user permissions.</p>
<p>If <tt class="docutils literal"><span class="pre">obj</span></tt> is passed in, only returns the permissions for this
specific object.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.PermissionsMixin.has_perm">
<tt class="descname">has_perm</tt>(<em>perm</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.auth.models.PermissionsMixin.has_perm" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if the user has the specified permission, where
<tt class="docutils literal"><span class="pre">perm</span></tt> is in the format <tt class="docutils literal"><span class="pre">&quot;&lt;app</span> <span class="pre">label&gt;.&lt;permission</span> <span class="pre">codename&gt;&quot;</span></tt> (see
<a class="reference internal" href="default.html#topic-authorization"><em>permissions</em></a>). If the user is inactive, this method will
always return <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
<p>If <tt class="docutils literal"><span class="pre">obj</span></tt> is passed in, this method won&#8217;t check for a permission for
the model, but for this specific object.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.PermissionsMixin.has_perms">
<tt class="descname">has_perms</tt>(<em>perm_list</em>, <em>obj=None</em>)<a class="headerlink" href="#django.contrib.auth.models.PermissionsMixin.has_perms" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if the user has each of the specified permissions,
where each perm is in the format
<tt class="docutils literal"><span class="pre">&quot;&lt;app</span> <span class="pre">label&gt;.&lt;permission</span> <span class="pre">codename&gt;&quot;</span></tt>. If the user is inactive,
this method will always return <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
<p>If <tt class="docutils literal"><span class="pre">obj</span></tt> is passed in, this method won&#8217;t check for permissions for
the model, but for the specific object.</p>
</dd></dl>

<dl class="method">
<dt id="django.contrib.auth.models.PermissionsMixin.has_module_perms">
<tt class="descname">has_module_perms</tt>(<em>package_name</em>)<a class="headerlink" href="#django.contrib.auth.models.PermissionsMixin.has_module_perms" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if the user has any permissions in the given package
(the Django app label). If the user is inactive, this method will
always return <tt class="docutils literal"><span class="pre">False</span></tt>.</p>
</dd></dl>

</dd></dl>

<div class="admonition-modelbackend admonition">
<p class="first admonition-title">ModelBackend</p>
<p class="last">If you don&#8217;t include the
<a class="reference internal" href="#django.contrib.auth.models.PermissionsMixin" title="django.contrib.auth.models.PermissionsMixin"><tt class="xref py py-class docutils literal"><span class="pre">PermissionsMixin</span></tt></a>, you must ensure you
don&#8217;t invoke the permissions methods on <tt class="docutils literal"><span class="pre">ModelBackend</span></tt>. <tt class="docutils literal"><span class="pre">ModelBackend</span></tt>
assumes that certain fields are available on your user model. If your User
model doesn&#8217;t provide  those fields, you will receive database errors when
you check permissions.</p>
</div>
</div>
<div class="section" id="s-custom-users-and-proxy-models">
<span id="custom-users-and-proxy-models"></span><h3>Custom users and Proxy models<a class="headerlink" href="#custom-users-and-proxy-models" title="Permalink to this headline">¶</a></h3>
<p>One limitation of custom User models is that installing a custom User model
will break any proxy model extending <a class="reference internal" href="../../ref/contrib/auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><tt class="xref py py-class docutils literal"><span class="pre">User</span></tt></a>.
Proxy models must be based on a concrete base class; by defining a custom User
model, you remove the ability of Django to reliably identify the base class.</p>
<p>If your project uses proxy models, you must either modify the proxy to extend
the User model that is currently in use in your project, or merge your proxy&#8217;s
behavior into your User subclass.</p>
</div>
<div class="section" id="s-custom-users-and-signals">
<span id="custom-users-and-signals"></span><h3>Custom users and signals<a class="headerlink" href="#custom-users-and-signals" title="Permalink to this headline">¶</a></h3>
<p>Another limitation of custom User models is that you can&#8217;t use
<a class="reference internal" href="#django.contrib.auth.get_user_model" title="django.contrib.auth.get_user_model"><tt class="xref py py-func docutils literal"><span class="pre">django.contrib.auth.get_user_model()</span></tt></a> as the sender or target of a signal
handler. Instead, you must register the handler with the resulting User model.
See <a class="reference internal" href="../signals.html"><em>Signals</em></a> for more information on registering and sending
signals.</p>
</div>
<div class="section" id="s-custom-users-and-testing-fixtures">
<span id="custom-users-and-testing-fixtures"></span><h3>Custom users and testing/fixtures<a class="headerlink" href="#custom-users-and-testing-fixtures" title="Permalink to this headline">¶</a></h3>
<p>If you are writing an application that interacts with the User model, you must
take some precautions to ensure that your test suite will run regardless of
the User model that is being used by a project. Any test that instantiates an
instance of User will fail if the User model has been swapped out. This
includes any attempt to create an instance of User with a fixture.</p>
<p>To ensure that your test suite will pass in any project configuration,
<tt class="docutils literal"><span class="pre">django.contrib.auth.tests.utils</span></tt> defines a <tt class="docutils literal"><span class="pre">&#64;skipIfCustomUser</span></tt> decorator.
This decorator will cause a test case to be skipped if any User model other
than the default Django user is in use. This decorator can be applied to a
single test, or to an entire test class.</p>
<p>Depending on your application, tests may also be needed to be added to ensure
that the application works with <em>any</em> user model, not just the default User
model. To assist with this, Django provides two substitute user models that
can be used in test suites:</p>
<dl class="class">
<dt id="django.contrib.auth.tests.custom_user.CustomUser">
<em class="property">class </em><tt class="descclassname">tests.custom_user.</tt><tt class="descname">CustomUser</tt><a class="headerlink" href="#django.contrib.auth.tests.custom_user.CustomUser" title="Permalink to this definition">¶</a></dt>
<dd><p>A custom user model that uses an <tt class="docutils literal"><span class="pre">email</span></tt> field as the username, and has a basic
admin-compliant permissions setup</p>
</dd></dl>

<dl class="class">
<dt id="django.contrib.auth.tests.custom_user.ExtensionUser">
<em class="property">class </em><tt class="descclassname">tests.custom_user.</tt><tt class="descname">ExtensionUser</tt><a class="headerlink" href="#django.contrib.auth.tests.custom_user.ExtensionUser" title="Permalink to this definition">¶</a></dt>
<dd><p>A custom user model that extends <tt class="docutils literal"><span class="pre">django.contrib.auth.models.AbstractUser</span></tt>,
adding a <tt class="docutils literal"><span class="pre">date_of_birth</span></tt> field.</p>
</dd></dl>

<p>You can then use the <tt class="docutils literal"><span class="pre">&#64;override_settings</span></tt> decorator to make that test run
with the custom User model. For example, here is a skeleton for a test that
would test three possible User models &#8211; the default, plus the two User
models provided by <tt class="docutils literal"><span class="pre">auth</span></tt> app:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.auth.tests.utils</span> <span class="kn">import</span> <span class="n">skipIfCustomUser</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.tests.custom_user</span> <span class="kn">import</span> <span class="n">CustomUser</span><span class="p">,</span> <span class="n">ExtensionUser</span>
<span class="kn">from</span> <span class="nn">django.test</span> <span class="kn">import</span> <span class="n">TestCase</span><span class="p">,</span> <span class="n">override_settings</span>


<span class="k">class</span> <span class="nc">ApplicationTestCase</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>
    <span class="nd">@skipIfCustomUser</span>
    <span class="k">def</span> <span class="nf">test_normal_user</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Run tests for the normal user model&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertSomething</span><span class="p">()</span>

    <span class="nd">@override_settings</span><span class="p">(</span><span class="n">AUTH_USER_MODEL</span><span class="o">=</span><span class="s">&#39;auth.CustomUser&#39;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">test_custom_user</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Run tests for a custom user model with email-based authentication&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertSomething</span><span class="p">()</span>

    <span class="nd">@override_settings</span><span class="p">(</span><span class="n">AUTH_USER_MODEL</span><span class="o">=</span><span class="s">&#39;auth.ExtensionUser&#39;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">test_extension_user</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Run tests for a simple extension of the built-in User.&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertSomething</span><span class="p">()</span>
</pre></div>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 1.6:</span> <p>In Django 1.5, it wasn&#8217;t necessary to explicitly import the test User models.</p>
</div>
</div>
<div class="section" id="s-a-full-example">
<span id="a-full-example"></span><h3>A full example<a class="headerlink" href="#a-full-example" title="Permalink to this headline">¶</a></h3>
<p>Here is an example of an admin-compliant custom user app. This user model uses
an email address as the username, and has a required date of birth; it
provides no permission checking, beyond a simple <tt class="docutils literal"><span class="pre">admin</span></tt> flag on the user
account. This model would be compatible with all the built-in auth forms and
views, except for the User creation forms. This example illustrates how most of
the components work together, but is not intended to be copied directly into
projects for production use.</p>
<p>This code would all live in a <tt class="docutils literal"><span class="pre">models.py</span></tt> file for a custom
authentication app:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">BaseUserManager</span><span class="p">,</span> <span class="n">AbstractBaseUser</span>
<span class="p">)</span>


<span class="k">class</span> <span class="nc">MyUserManager</span><span class="p">(</span><span class="n">BaseUserManager</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">create_user</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">email</span><span class="p">,</span> <span class="n">date_of_birth</span><span class="p">,</span> <span class="n">password</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Creates and saves a User with the given email, date of</span>
<span class="sd">        birth and password.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">email</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;Users must have an email address&#39;</span><span class="p">)</span>

        <span class="n">user</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span>
            <span class="n">email</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">normalize_email</span><span class="p">(</span><span class="n">email</span><span class="p">),</span>
            <span class="n">date_of_birth</span><span class="o">=</span><span class="n">date_of_birth</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="n">user</span><span class="o">.</span><span class="n">set_password</span><span class="p">(</span><span class="n">password</span><span class="p">)</span>
        <span class="n">user</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">user</span>

    <span class="k">def</span> <span class="nf">create_superuser</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">email</span><span class="p">,</span> <span class="n">date_of_birth</span><span class="p">,</span> <span class="n">password</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Creates and saves a superuser with the given email, date of</span>
<span class="sd">        birth and password.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">user</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_user</span><span class="p">(</span><span class="n">email</span><span class="p">,</span>
            <span class="n">password</span><span class="o">=</span><span class="n">password</span><span class="p">,</span>
            <span class="n">date_of_birth</span><span class="o">=</span><span class="n">date_of_birth</span>
        <span class="p">)</span>
        <span class="n">user</span><span class="o">.</span><span class="n">is_admin</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="n">user</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">user</span>


<span class="k">class</span> <span class="nc">MyUser</span><span class="p">(</span><span class="n">AbstractBaseUser</span><span class="p">):</span>
    <span class="n">email</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">EmailField</span><span class="p">(</span>
        <span class="n">verbose_name</span><span class="o">=</span><span class="s">&#39;email address&#39;</span><span class="p">,</span>
        <span class="n">max_length</span><span class="o">=</span><span class="mi">255</span><span class="p">,</span>
        <span class="n">unique</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">date_of_birth</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
    <span class="n">is_active</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">is_admin</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

    <span class="n">objects</span> <span class="o">=</span> <span class="n">MyUserManager</span><span class="p">()</span>

    <span class="n">USERNAME_FIELD</span> <span class="o">=</span> <span class="s">&#39;email&#39;</span>
    <span class="n">REQUIRED_FIELDS</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;date_of_birth&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">get_full_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># The user is identified by their email address</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">email</span>

    <span class="k">def</span> <span class="nf">get_short_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># The user is identified by their email address</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">email</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>              <span class="c"># __unicode__ on Python 2</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">email</span>

    <span class="k">def</span> <span class="nf">has_perm</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">perm</span><span class="p">,</span> <span class="n">obj</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="s">&quot;Does the user have a specific permission?&quot;</span>
        <span class="c"># Simplest possible answer: Yes, always</span>
        <span class="k">return</span> <span class="bp">True</span>

    <span class="k">def</span> <span class="nf">has_module_perms</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">app_label</span><span class="p">):</span>
        <span class="s">&quot;Does the user have permissions to view the app `app_label`?&quot;</span>
        <span class="c"># Simplest possible answer: Yes, always</span>
        <span class="k">return</span> <span class="bp">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_staff</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Is the user a member of staff?&quot;</span>
        <span class="c"># Simplest possible answer: All admins are staff</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_admin</span>
</pre></div>
</div>
<p>Then, to register this custom User model with Django&#8217;s admin, the following
code would be required in the app&#8217;s <tt class="docutils literal"><span class="pre">admin.py</span></tt> file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="n">Group</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.admin</span> <span class="kn">import</span> <span class="n">UserAdmin</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth.forms</span> <span class="kn">import</span> <span class="n">ReadOnlyPasswordHashField</span>

<span class="kn">from</span> <span class="nn">customauth.models</span> <span class="kn">import</span> <span class="n">MyUser</span>


<span class="k">class</span> <span class="nc">UserCreationForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">ModelForm</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A form for creating new users. Includes all the required</span>
<span class="sd">    fields, plus a repeated password.&quot;&quot;&quot;</span>
    <span class="n">password1</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="s">&#39;Password&#39;</span><span class="p">,</span> <span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">PasswordInput</span><span class="p">)</span>
    <span class="n">password2</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="s">&#39;Password confirmation&#39;</span><span class="p">,</span> <span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">PasswordInput</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">MyUser</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;email&#39;</span><span class="p">,</span> <span class="s">&#39;date_of_birth&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">clean_password2</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Check that the two password entries match</span>
        <span class="n">password1</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;password1&quot;</span><span class="p">)</span>
        <span class="n">password2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;password2&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">password1</span> <span class="ow">and</span> <span class="n">password2</span> <span class="ow">and</span> <span class="n">password1</span> <span class="o">!=</span> <span class="n">password2</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">forms</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span><span class="s">&quot;Passwords don&#39;t match&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">password2</span>

    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">commit</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
        <span class="c"># Save the provided password in hashed format</span>
        <span class="n">user</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">UserCreationForm</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">commit</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
        <span class="n">user</span><span class="o">.</span><span class="n">set_password</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;password1&quot;</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">commit</span><span class="p">:</span>
            <span class="n">user</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">user</span>


<span class="k">class</span> <span class="nc">UserChangeForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">ModelForm</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A form for updating users. Includes all the fields on</span>
<span class="sd">    the user, but replaces the password field with admin&#39;s</span>
<span class="sd">    password hash display field.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">password</span> <span class="o">=</span> <span class="n">ReadOnlyPasswordHashField</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">MyUser</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;email&#39;</span><span class="p">,</span> <span class="s">&#39;password&#39;</span><span class="p">,</span> <span class="s">&#39;date_of_birth&#39;</span><span class="p">,</span> <span class="s">&#39;is_active&#39;</span><span class="p">,</span> <span class="s">&#39;is_admin&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">clean_password</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Regardless of what the user provides, return the initial value.</span>
        <span class="c"># This is done here, rather than on the field, because the</span>
        <span class="c"># field does not have access to the initial value</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial</span><span class="p">[</span><span class="s">&quot;password&quot;</span><span class="p">]</span>


<span class="k">class</span> <span class="nc">MyUserAdmin</span><span class="p">(</span><span class="n">UserAdmin</span><span class="p">):</span>
    <span class="c"># The forms to add and change user instances</span>
    <span class="n">form</span> <span class="o">=</span> <span class="n">UserChangeForm</span>
    <span class="n">add_form</span> <span class="o">=</span> <span class="n">UserCreationForm</span>

    <span class="c"># The fields to be used in displaying the User model.</span>
    <span class="c"># These override the definitions on the base UserAdmin</span>
    <span class="c"># that reference specific fields on auth.User.</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;email&#39;</span><span class="p">,</span> <span class="s">&#39;date_of_birth&#39;</span><span class="p">,</span> <span class="s">&#39;is_admin&#39;</span><span class="p">)</span>
    <span class="n">list_filter</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;is_admin&#39;</span><span class="p">,)</span>
    <span class="n">fieldsets</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;email&#39;</span><span class="p">,</span> <span class="s">&#39;password&#39;</span><span class="p">)}),</span>
        <span class="p">(</span><span class="s">&#39;Personal info&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;date_of_birth&#39;</span><span class="p">,)}),</span>
        <span class="p">(</span><span class="s">&#39;Permissions&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;is_admin&#39;</span><span class="p">,)}),</span>
    <span class="p">)</span>
    <span class="c"># add_fieldsets is not a standard ModelAdmin attribute. UserAdmin</span>
    <span class="c"># overrides get_fieldsets to use this attribute when creating a user.</span>
    <span class="n">add_fieldsets</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="p">{</span>
            <span class="s">&#39;classes&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;wide&#39;</span><span class="p">,),</span>
            <span class="s">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;email&#39;</span><span class="p">,</span> <span class="s">&#39;date_of_birth&#39;</span><span class="p">,</span> <span class="s">&#39;password1&#39;</span><span class="p">,</span> <span class="s">&#39;password2&#39;</span><span class="p">)}</span>
        <span class="p">),</span>
    <span class="p">)</span>
    <span class="n">search_fields</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;email&#39;</span><span class="p">,)</span>
    <span class="n">ordering</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;email&#39;</span><span class="p">,)</span>
    <span class="n">filter_horizontal</span> <span class="o">=</span> <span class="p">()</span>

<span class="c"># Now register the new UserAdmin...</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">MyUser</span><span class="p">,</span> <span class="n">MyUserAdmin</span><span class="p">)</span>
<span class="c"># ... and, since we&#39;re not using Django&#39;s built-in permissions,</span>
<span class="c"># unregister the Group model from admin.</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">unregister</span><span class="p">(</span><span class="n">Group</span><span class="p">)</span>
</pre></div>
</div>
<p>Finally, specify the custom model as the default user model for your project
using the <a class="reference internal" href="../../ref/settings.html#std:setting-AUTH_USER_MODEL"><tt class="xref std std-setting docutils literal"><span class="pre">AUTH_USER_MODEL</span></tt></a> setting in your <tt class="docutils literal"><span class="pre">settings.py</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">AUTH_USER_MODEL</span> <span class="o">=</span> <span class="s">&#39;customauth.MyUser&#39;</span>
</pre></div>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Customizing authentication in Django</a><ul>
<li><a class="reference internal" href="#other-authentication-sources">Other authentication sources</a><ul>
<li><a class="reference internal" href="#specifying-authentication-backends">Specifying authentication backends</a></li>
<li><a class="reference internal" href="#writing-an-authentication-backend">Writing an authentication backend</a></li>
<li><a class="reference internal" href="#handling-authorization-in-custom-backends">Handling authorization in custom backends</a><ul>
<li><a class="reference internal" href="#authorization-for-anonymous-users">Authorization for anonymous users</a></li>
<li><a class="reference internal" href="#authorization-for-inactive-users">Authorization for inactive users</a></li>
<li><a class="reference internal" href="#handling-object-permissions">Handling object permissions</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#custom-permissions">Custom permissions</a></li>
<li><a class="reference internal" href="#extending-the-existing-user-model">Extending the existing User model</a></li>
<li><a class="reference internal" href="#substituting-a-custom-user-model">Substituting a custom User model</a><ul>
<li><a class="reference internal" href="#referencing-the-user-model">Referencing the User model</a></li>
<li><a class="reference internal" href="#specifying-a-custom-user-model">Specifying a custom User model</a></li>
<li><a class="reference internal" href="#extending-django-s-default-user">Extending Django&#8217;s default User</a></li>
<li><a class="reference internal" href="#custom-users-and-the-built-in-auth-forms">Custom users and the built-in auth forms</a></li>
<li><a class="reference internal" href="#custom-users-and-django-contrib-admin">Custom users and <tt class="docutils literal"><span class="pre">django.contrib.admin</span></tt></a></li>
<li><a class="reference internal" href="#custom-users-and-permissions">Custom users and permissions</a></li>
<li><a class="reference internal" href="#custom-users-and-proxy-models">Custom users and Proxy models</a></li>
<li><a class="reference internal" href="#custom-users-and-signals">Custom users and signals</a></li>
<li><a class="reference internal" href="#custom-users-and-testing-fixtures">Custom users and testing/fixtures</a></li>
<li><a class="reference internal" href="#a-full-example">A full example</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="passwords.html">Password management in Django</a></li>
    
    
      <li>Next: <a href="../cache.html">Django&#8217;s cache framework</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">Using Django</a>
        
          <ul><li><a href="index.html">User authentication in Django</a>
        
        <ul><li>Customizing authentication in Django</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/topics/auth/customizing.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="passwords.html" title="Password management in Django">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="../cache.html" title="Django&amp;#8217;s cache framework">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>